-- @owner: @kyeleze
-- @date: 2022/5/13
-- @testpoint: 自定义implicit转换关系存在，系统表pg_cast中查看;自定义转换关系属性与系统表相符

--step1:建立自定义类型转换函数;expect:创建函数成功
create or replace function timezone_to_secondsinteger
(
    in time with time zone
)
returns integer
as
$$
declare
seconds integer;
begin
select (
extract(hour from $1)::integer * 3600
+ extract(minute from $1)::integer * 60
+ extract(seconds from $1)::integer* 1) into seconds;
return seconds;
end;
$$ language plpgsql;
/
--step2:创建自定义转换关系;expect:创建成功
drop cast if exists (time with time zone as integer) cascade;
create cast(time with time zone as integer) with function timezone_to_secondsinteger(time with time zone) as implicit;

--step3:系统表pg_cast中查看;expect:castcontext为i,castmethod字段为f
select castcontext, castmethod from pg_cast where castfunc in (select oid from  pg_proc where proname='timezone_to_secondsinteger');

--step4:清理环境;expect:清理成功
drop cast if exists (time with time zone as integer) cascade;
drop function if exists timezone_to_secondsinteger(time with time zone) cascade;
